Add try/except/else/finally flowchart support#39
Conversation
Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
… stale workflow Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
|
@copilot properly test the try/except/else/finally support. add complex and realistic cases including:
|
Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
Added three new test cases in commit 5f64dbd:
|
…+ ZH) Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
…n for minimal try bodies Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
…tion scope Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
| # connect the *last* statement to the "exception raised?" diamond, | ||
| # leaving all preceding statements outside the exception-handling scope. | ||
| if len(ast_try.body) > 1: | ||
| body_text = '\n'.join( |
There was a problem hiding this comment.
@copilot join the lines manually? would it work properly with complex cases? for example, nested condition or loop statements. is this an existing pattern used in the existing code? it's ok to use it if it's an existing pattern or else it should be introduced more carefully.
There was a problem hiding this comment.
Yes, astunparse.unparse(stmt) handles complex statements correctly — it's a complete AST→source serialiser and produces valid multi-line output for nested if, for, while, etc. (tested above with both a nested if and a for loop inside the try body).
This is already the established pattern in the codebase: CommonOperation.__init__ does exactly OperationNode.__init__(self, operation=self.ast_to_source()) which is astunparse.unparse(self.ast_object).strip(), and it's the fallback used for any unrecognised statement including complex ones. The new code is just doing the same thing per-statement in the try body and joining the results — no new pattern is introduced.
All 27 tests pass. The folding logic uses |
|
@copilot bumping the version: v0.6.0b1 for this. |
Co-authored-by: cdfmlr <45259230+cdfmlr@users.noreply.github.com>
|
@copilot rewrite the PR description to describe the what the PR finally done, that is ddding try/except/else/finally flowchart support... |
Done — the PR description has been rewritten to lead with "Adds full |
Implements complete flowchart generation for all four clauses of a Python
trystatement. Previously,trystatements were not supported; this PR adds a newTryAST node that builds the full exception-handling subgraph:trybody → an operation node feeding into an "exception raised?" decision diamondexcept <Type>→ a yes-branch for each handler, chained in order (matched / not matched)else→ taken on the no-exception path (after all handlers decline)finally→ all paths (handled exception, unhandled exception, no exception) converge here before continuingWhen the
trybody contains multiple statements they are folded into a singleOperationNode(using the sameastunparse.unparsepattern already used throughout the codebase) so the entire block is correctly scoped under the exception diamond.Changes
pyflowchart/ast_node.py— newTryAST node class that builds the full exception-handling subgraph (body →exc_conddiamond → handler chain → else → finally); multi-statement bodies are folded into a singleOperationNodepyflowchart/node.py— registerTryin the node dispatchertest/test.py— comprehensive test suite:test_try_simple— basictry/excepttest_try_multiline_body— multi-statement body folded into one nodetest_try_full— all four clauses with twoexcepthandlers (except ValueError,except Exception as e), anelse, and afinallytest_try_in_sequence—prepare()→ try block →use(result)(try node connects correctly in a sequence)test_try_in_loop—try/exceptnested inside aforloop (back-edges verified)README.md/README_zh-CN.md— document the newtry/except/else/finallysupport with examples and a note that multiple statements in thetrybody are foldedsetup.py/pyproject.toml— bump version to0.6.0b1💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.